﻿//<reference name="MicrosoftAjax.js" />

function Tags(ParentControl, TagsOwner){
    this.TagList=new Array();
    this.AllowAdd=true;
    this.AllowDelete=true;
    this.Seperator=" [x] ";
    this.ParentControl=ParentControl;
    this.TagsOwner=TagsOwner;
    this.DefaultOnClientClick='null';
}

Tags.prototype.Add = function(Tag) {
    //allow these items to be inherited from the parent
    Tag.AllowAdd = this.AllowAdd;
    Tag.AllowDelete = this.AllowDelete;
    Tag.Seperator = this.Seperator;
    if(Tag.OnClientClick==null || Tag.OnClientClick=="" || Tag.OnClientClick=="null") Tag.OnClientClick = this.DefaultOnClientClick;
    this.TagList.push(Tag);
    Tag.Parent=this;
    Tag.Render(this.ParentControl, this.TagsOwner);
    return Tag;
}

Tags.prototype.ShowAddInput = function() {
    var newTag = prompt("New Tag Name", "Tag...");
    if(newTag) this.Add(new Tag(newTag, 0, "medium", true, this.DefaultOnClientClick));
    //Tag.Render(this.ParentControl, this.TagsOwner);
}

Tags.prototype.Remove = function(TagName) {
    for(var t in this.TagList) {
        var tag = this.TagList[t];
        if(tag.Name==TagName) {
            tag.ParentControl.style.display="none";
            tag.XControl.style.display="none";
            tag.Enabled=false;
        }
    }
}
Array.remove = function(array, from, to) {
  var rest = array.slice((to || from) + 1 || array.length);
  array.length = from < 0 ? array.length + from : from;
  return array.push.apply(array, rest);
};

function Tag(Name, Count, FontSize, Enabled, OnClientClick) {
    this.Name=Name;
    this.Count=Count;
    this.FontSize=FontSize;
    this.ParentControl=null;
    this.XControl=null;
    this.Enabled=Enabled;
    if(this.Enabled==null) this.Enabled=true;
    this.AllowAdd=true;
    this.AllowDelete=true;
    this.Seperator=" [x] ";
    this.Parent=null;
    this.OnClientClick=OnClientClick;
}


Tag.prototype.Render = function(ParentControl, TagsOwner) {
        
        var link = document.createElement("span");
        //link.innerText = this.Name;
        link.appendChild(document.createTextNode(this.Name));
        if(this.OnClientClick) link.onclick = this.OnClientClick;

        
        link.style.fontSize=this.FontSize;
        link.Tag = this;
        if(this.OnClientClick!="null") {
            link.onclick = this.HandleClientClick;
            link.style.cursor="pointer";
        }
        
        
        var elm = document.createElement("span");
        elm.appendChild(document.createTextNode(this.Seperator));
        if(this.AllowDelete) elm.style.cursor="pointer";
        elm.style.color="#C0C0C0"
        elm.style.fontSize="xx-small";       
        if(this.AllowDelete) elm.onclick = this.RemoveTag;
        elm.Tag = this;
        elm.TagsOwner = TagsOwner;
        
        
        ParentControl.appendChild(link);
        ParentControl.appendChild(elm);
        
        this.ParentControl=link;
        this.XControl=elm;       
         
        if(!this.Enabled) {
            this.ParentControl.style.display="none";
            this.XControl.style.display="none";
        }
}
Tag.prototype.HandleClientClick = function(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "");
    if (evt) {
        var elm = (evt.originalTarget!=null)?evt.originalTarget:evt.srcElement;  
        var Tag = elm.Tag;
        if( elm.Tag.OnClientClick!="" && elm.Tag.OnClientClick!=null && elm.Tag.OnClientClick!="null") eval(elm.Tag.OnClientClick);
    }
}
Tag.prototype.RemoveTag = function(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "");
    if (evt) {
        var elm = (evt.originalTarget!=null)?evt.originalTarget:evt.srcElement;                
        elm.Tag.Parent.Remove(elm.Tag.Name);
    }
}
